package com.dongdongshop.controller;

import com.dongdongshop.exception.LoginSellerException;
import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbSeller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("sellerLogin")
public class LoginSellerController {
    Logger logger = LoggerFactory.getLogger(LoginSellerController.class);
    @RequestMapping("getLogin")
    @ResponseBody
    public AjaxResult getLogin(TbSeller seller){
        /*通过shrio认证登录*/
        Subject subject = SecurityUtils.getSubject();
        /*封装用户数据*/
        UsernamePasswordToken token = new UsernamePasswordToken(seller.getSellerId(),seller.getPassword());
        try {
            subject.login(token);
            /*登陆成功跳转首页*/
            return AjaxResult.R(ResultEnum.SUCCESS);
        }catch (UnknownAccountException e){
            /*用户名不存在*/
            System.out.println("用户名不存在");
            logger.error("登录:用户名不存在!错误内容:{}",e);
            return AjaxResult.R(ResultEnum.UNKNOWNACCOUNTEXCEPTION);
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
            logger.error("登录:密码错误!错误内容:{}",e);
            return AjaxResult.R(ResultEnum.INCORRECTCREDENTIALSEXCEPTION);
        }catch (LoginSellerException lse){
            System.out.println("账号审核未通过或存在异常!");
            logger.error("登录:账号审核未通过或存在异常!!错误内容:{}",lse);
            return AjaxResult.R(ResultEnum.LOGINEXCEPTION);
        }
    }

    /*跳转首页*/
    @RequestMapping("shopIndex")
    public String shopIndex(){
        return "/admin/index";
    }

    /*跳转注册*/
    @RequestMapping("shopRegister")
    public String shopRegister(){
        return "register";
    }

    @RequestMapping("shopLogin")
    public String shopLogin(){
        return "shoplogin";
    }
}
